const app = getApp();
Page({

  data: {
    // good:{},
    cartList: [],
    goodComment: [],
    numTotal: 1,
    toastComment: '暂无评论',
    numberMax: 1
  },
  onLoad: function (options) {
    console.log(options);
    // let selectId=options.goodsId;
    // 根据传过来的id来查询商品详情数据
    wx.request({
      url: `${app.globalData.baseurl}goods/goodsDetail`,
      method: 'POST',
      data: {
        goodsid: options.goodsId,
      },
      success: (res) => {
        if (res.statusCode == 200) {
          this.setData({
            good: res.data[0]
          });
          console.log(this.data.good);
        }
      },
      fail: (err) => {
        console.log('error!');
      }
    })
    // 根据传过来的id来查询商品评价信息
    wx.request({
      url: `${app.globalData.baseurl}goods/goodsComment`,
      method: 'POST',
      data: {
        goodsid: options.goodsId,
        // comment:'评价内容'
      },
      success: (res) => {
        if (res.statusCode == 200) {
          this.setData({
            goodComment: res.data
          });
          console.log(this.data.goodComment);
        }
      },
      fail: (err) => {
        console.log('error!');
      }
    })
    // 设置购物车商品数量
    // console.log(app.globalData.cartList);
    this.setData({
      cartList: app.globalData.cartList
    })
  },
  // 分享给好友
  onShareAppMessage() {
    return {
      title: this.data.good.goodsname,
      path: '/pages/goodDetail/goodDetail?goodsId=' + this.data.good.goodsid,
      imageUrl: this.data.good.goodsimg
    }
  },
  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: this.data.good.goodsname,
      query: {
        goodsId: this.data.good.goodsid
      },
      imageUrl: this.data.good.goodsimg
    }
  },
  // 添加当前商品到购物车
  addCart() {
    let cartList = wx.getStorageSync('cartList');
    let index = -1;
    console.log(this.data.good.goodsnum);
    if (this.data.good.goodsnum > 0) {
      if (cartList.length == 0) {
        this.data.good.number = 1
        //默认是选中状态
        this.data.good.choose = true
        app.globalData.cartList.push(this.data.good);
        wx.setStorageSync('cartList', app.globalData.cartList);
        wx.showToast({
          title: '添加成功',
        })
      } else {
        for (let idx in cartList) {
          // console.log(idx);
          if (cartList[idx].goodsid == this.data.good.goodsid) {
            index = idx;
          }
        }
        if (index != -1) {
          wx.showToast({
            title: '已加入购物车',
            icon: 'none'
          })
        } else {
          this.data.good.number = 1;
          this.data.good.choose = true;
          app.globalData.cartList.push(this.data.good);
          wx.setStorageSync('cartList', app.globalData.cartList);
          wx.showToast({
            title: '添加成功',
          })
        }
      }
      this.setData({
        cartList: app.globalData.cartList
      })
    } else {
      wx.showToast({
        icon: 'none',
        title: '商品已售罄！无法加入购物车',
      })
    }

  },
  toOrder() {
    if (this.data.good.goodsnum > 0) {
      let orderList = []
      orderList.push(this.data.good)
      orderList[0].number = 1;
      app.globalData.orderList = orderList;
      app.globalData.sumPrice = this.data.good.secprice
      app.globalData.numTotal = 1;
      numTotal = app.globalData.numTotal;
      if (app.globalData.userInfo == null) {
        wx.showToast({
          icon: 'error',
          title: '请先登录',
        })
        return
      }
      wx.navigateTo({
        url: '../order/order',
      })
    }else{
      wx.showToast({
        icon: 'none',
        title: '商品已售罄！无法购买',
      })
    }

  }
})